package cn.px.rest.controller.repair;

import cn.px.common.bean.AbstractPageResultBean;
import cn.px.common.exception.BusinessException;
import cn.px.common.util.BeanConverterUtil;
import cn.px.common.util.IdGeneratorUtils;
import cn.px.common.util.R;
import cn.px.common.validation.group.AddGroup;
import cn.px.common.validation.group.UpdateGroup;
import cn.px.dao.dto.repair.RepairProjectRelationDTO;
import cn.px.dao.dto.sb.SbModelSpareBomDTO;
import cn.px.dao.entity.repair.RepairProjectRelation;
import cn.px.dao.util.ExcelUtil;
import cn.px.dao.vo.export.lubricationmanage.ExportLubricationPlanStandardRelationVO;
import cn.px.dao.vo.repair.RepairProjectVO;
import cn.px.rest.log.annotation.SysLog;
import cn.px.service.repair.RepairProjectRelationService;
import cn.px.service.repair.RepairProjectService;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Description 型号库-备件bom关联 控制器
 * @author 品讯科技
 * @date 2024-08
 * @Version Copyright (c) 2019,北京乾元坤和科技有限公司 All rights reserved.
 */
@RestController
@AllArgsConstructor
@RequestMapping("/repair/project-relations")
public class RepairProjectRelationController {

  private final RepairProjectRelationService sbModelSpareBomService;

  private final RepairProjectService sparePartInfoService;

  /**
   * 通过id查询单条记录
   *
   * @param id 主键
   * @return R
   */
  @GetMapping("/{id}")
  public R<RepairProjectRelation> getById(@PathVariable("id") String id){
      return new R<>(sbModelSpareBomService.getModelById(id));
  }

  /**
   * 新增记录
   *
   * @param sbModelSpareBomDTO 维修单-维修项目关联DTO
   * @return R
   */
  @SysLog("新增维修单-维修项目关联")
  @PostMapping
  public R save(@Validated({AddGroup.class}) @RequestBody RepairProjectRelationDTO sbModelSpareBomDTO) throws Exception {
      RepairProjectRelationDTO d = new RepairProjectRelationDTO();
      d.setRepairId(sbModelSpareBomDTO.getRepairId());
      d.setProjectId(sbModelSpareBomDTO.getProjectId());
      int count = sbModelSpareBomService.getCountByDTO(d);
      if(count != 0){
          throw new BusinessException("已经存在该维修项目，请重新选择");
      }
      return new R<>(sbModelSpareBomService.saveModelByDTO(sbModelSpareBomDTO));
  }

    /**
     * 批量新增记录
     *
     * @param sbModelSpareBomDTOList 维修单-维修项目关联DTO
     * @return R
     */
    @SysLog("新增维修单-维修项目关联")
    @PostMapping("/batch")
    public R save(@Validated({AddGroup.class}) @RequestBody List<RepairProjectRelationDTO> sbModelSpareBomDTOList) throws Exception {

        for(RepairProjectRelationDTO sbModelSpareBomDTO:sbModelSpareBomDTOList ){
            RepairProjectRelationDTO d = new RepairProjectRelationDTO();
            d.setRepairId(sbModelSpareBomDTO.getRepairId());
            d.setProjectId(sbModelSpareBomDTO.getProjectId());
            int count = sbModelSpareBomService.getCountByDTO(d);
            if(count != 0){
                throw new BusinessException("已经存在该维修项目，请重新选择");
            }
            sbModelSpareBomDTO.setId(IdGeneratorUtils.getObjectId());
        }
        sbModelSpareBomService.saveModelListByDTO(sbModelSpareBomDTOList);
        return new R<>("批量添加成功");
    }

    /**
     * 导入设备型号BOM
     *
     * @param file bom excel文件
     * @param modelId 设备型号
     * @return R
     */
    @SysLog("导入维修单-维修项目关联")
    @PostMapping("/import")
    public R importExcel(@RequestParam("file") MultipartFile file, String modelId ) throws Exception {
        //sbModelSpareBomService.importList(file, modelId);
        return new R<>("导入成功");
    }

  /**
   * 修改记录
   *
   * @param sbModelSpareBomDTO 维修单-维修项目关联DTO
   * @return R
   */
  @SysLog("修改维修单-维修项目关联")
  @PutMapping("/{id}")
  public R update(@PathVariable("id") String id, @Validated({UpdateGroup.class}) @RequestBody RepairProjectRelationDTO sbModelSpareBomDTO) {
      sbModelSpareBomService.modModelByDTO(sbModelSpareBomDTO);
      return new R<>();
  }

  /**
   * 通过id删除一条记录
   *
   * @param id 主键
   * @return R
   */
  @SysLog("删除维修单-维修项目关联")
  @DeleteMapping("/{id}")
  public R removeById(@PathVariable String id){
    sbModelSpareBomService.deleteByPrimaryKey(id);
    return new R<>();
  }

    /**
     * 批量记录
     *
     * @param ids 主键
     * @return R
     */
    @SysLog("批量删除维修单-维修项目关联")
    @DeleteMapping("")
    public R removeIds(@RequestBody List<String> ids){
        sbModelSpareBomService.batchDelete(ids);
        return new R<>();
    }

  /**
   * 获取分页
   *
   * @param pageNum 当前页码
   * @param pageSize 每页条数
   * @param sbModelSpareBomDTO 维修单-维修项目关联DTO
   * @return R
   */
  @GetMapping("/page")
  public R<AbstractPageResultBean<RepairProjectRelation>> query(RepairProjectRelationDTO sbModelSpareBomDTO, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "20") int pageSize) {
      return new R<>(sbModelSpareBomService.selectPageInfo(sbModelSpareBomDTO, pageNum, pageSize));
  }

  /**
   * 获取列表
   *
   * @param sbModelSpareBomDTO 维修单-维修项目关联DTO
   * @return R
   */
  @GetMapping("")
  public R query(RepairProjectRelationDTO sbModelSpareBomDTO) {
      return new R<>(sbModelSpareBomService.getModelListByDTO(sbModelSpareBomDTO));
  }

  /**
     * 维修单-维修项目关联导出
     *
     * @param sbModelSpareBomDTO 维修单-维修项目关联DTO
     * @return R
     */
  @GetMapping("/export")
  @SysLog("维修单-维修项目关联导出")
  public void export(HttpServletResponse response, RepairProjectRelationDTO sbModelSpareBomDTO) {
    List<RepairProjectRelation> list = sbModelSpareBomService.getModelListByDTO(sbModelSpareBomDTO);
    ExcelUtil.exportResponseDict(response, ExportLubricationPlanStandardRelationVO.class, BeanConverterUtil.copyListProperties(list, ExportLubricationPlanStandardRelationVO.class), "维修单-维修项目关联");
  }

  /**
   * 关联备件
   *
   * @param repairId 维修单-维修项目关联DTO
   * @return R
   */
  @SysLog("维修项目")
  @GetMapping("repair/{repairId}")
 public R selectRepairProjectListByRepairId(@PathVariable String repairId) {
    List<RepairProjectVO> list =  sparePartInfoService.selectRepairProjectListByRepairId(repairId);
    return new R<>(list);
  }

  /**
   * 批量取消维修单-维修项目关联
   *
   * @param sbModelSpareBomDTO 维修单-维修项目关联DTO
   * @return R
   */
  @SysLog("批量删除维修项目")
  @DeleteMapping("batchCancelRelate")
  public R removeIds(@RequestBody SbModelSpareBomDTO sbModelSpareBomDTO){
    sbModelSpareBomService.batchCancelRelate(sbModelSpareBomDTO);
    return new R<>();
  }

}
